import re

import requests

import configReader
import stories
from requests.cookies import cookiejar_from_dict
from bs4 import BeautifulSoup


def getSearchId(host, departure, arrival, date):
    # 定义请求的URL
    url = 'https://' + host + '/flights/' + departure + '-' + arrival + '/' + date + '?sort=bestflight_a'

    # 定义请求头
    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'cache-control': 'max-age=0',
        'priority': 'u=0, i',
        'referer': url,
        'cookie': configReader.getCookie(),
        'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126", "Google Chrome";v="126"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
        'x-oagw-debugmode': 'true',
        # 'cookies': 'Apache=JAe5wPABAPpQVAD$$n1T_A-AAABkOh1IzY-c9-PM0hNw; kanid=; kanlabel=; kayak=itL4XI2A_6L4uBjzvT$H; csid=8cb86090-105c-4c17-a73d-00e9e78ab1ae; kmkid=Ab_SgW4Y3de9qvgytJWVnUs; g_state={"i_l":1,"i_p":1721894204421}; cluster=4; p1.med.sid=R-4qmCKD$GHlzRtW4eVvYKh-bxnDu_WpnGF_sFknw5j3i2fjmmR_rZTMgm7u_ME3d; kayak.mc=Aep_RT-hp3Np9YU7sXyM0V7UbKk087TSLsYYLThqqk0LcUSLWQ1ZR9OFLUNVgVE5K6wYBkGoGYwFAzhLOqp4hng4mHUjb4nSCMjW6CV234AeVS6sK07RQbVZ1hnqA6FNtMxDfUohMs2erSSRoQo7F-ahlmqw_mfeu2fMI4KIFzdDTMyHHTAreqwZ9M_AE94o3gjhomZ53dDeC_9hji_01viwts77WF8zz6nZdNWxrbHfQ68f_bpI5UYYJYyKol59FSJG6Y0Imy2C8b_Pq8A4DUCXBcMVScgpXYGwLyFrv74UNcl6yz212dZ_z54qIcx-pBa-nsKYUHWVHwdTX9yOmQ8UUFxBjRmVGfjBU-nAeO_zu0yrRjX1M5XZ4TnHEjYusNeC7zn5Q1MusuFBJb_Ln6QtEj-UVqoWzz-fIzK1MvaWcWhOcdImETPzxDicU_RsNgZrFs-UV449lxLsMlcYbDs; __gads=ID=5ba4f4fabfd4d7cf:T=1721887021:RT=1721895904:S=ALNI_MYWSmZe5RQ01GcQEad1OoOTbXnDNQ; __gpi=UID=00000ea594803fdb:T=1721887021:RT=1721895904:S=ALNI_MbtPO2v7_uiO3e6iVM_5Q8JH_9cHA; __eoi=ID=c4ae3630745c3586:T=1721887021:RT=1721895904:S=AA-AfjZWdj0949ow2EFWCxB-tCfK; FCNEC=%5B%5B%22AKsRol9h4U3pOorg2NIoeA6icIz3VZ4tPI1Kx_xIGPMsXbPnkoKLxqS7BvWRxUop84ctbEcFhtJ4q7nRHgJJSKJE-0B1C_ExWprtzkz5V8_3YgNk95TpeyHUXA7-J6LM70f6OlewovKypStiIVDhrWx1KUppaijqIg%3D%3D%22%5D%5D; forterToken=5a0bcf1eca3f4695981dcf44e9e3aa5d_1721896084198__UDF43-m4_21ck; mst_iBfK2g=wfUn7KkVxwUhT_Fv8_sv9OlPlz1kMIoAopVcAJ1YdFZNrhfNS4e17tdiyzZYypHk39CPFTLhybmo2hi9e5AZdVRoycXk_n6dNevaj55qvWA; mst_ADIrkw=AlRnggQKhy3dHWQzMMB9uulPlz1kMIoAopVcAJ1YdFZNrhfNS4e17tdiyzZYypHkZC1qb5tymMEeMuhdJJZyyaD4G33NUyuuFYbyFGWUdrs'
    }

    # 发送GET请求
    response = requests.get(url, headers=headers)

    # 解析网页内容
    soup = BeautifulSoup(response.text, 'html.parser')
    # 找到id为__R9_HYDRATE_DATA__的script标签
    script_tag = soup.find('script', {'type': 'application/json'})
    # 检查是否找到了标签，并打印其内容
    if script_tag:
        # 获取标签内的文本内容
        script_content = script_tag.text
        print(script_content)
    else:
        print("没有找到对应的script标签。")

    search_id_pattern = r'"searchId":\s*"([^"]+)"'

    # 在HTML内容中搜索searchId
    match = re.search(search_id_pattern, response.text)

    if match:
        search_id_value = match.group(1)
        # print('searchId:', search_id_value)
        return search_id_value
    else:
        print('searchId not found')
        return ''

